//
//  HWVipHeadPortraitFrameController.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/10.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView
class HWVipHeadPortraitFrameController: HWBaseViewController {
    
    var tableView:UITableView!
    var tableViewProtocol = HWVipHeadProtraitFrameProtocol()
    var type = -1
    var httpManager = HWVIPHttpManager()
    var modelArr = NSMutableArray()
    var callBack:(()->())?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        
        initFrame()
        
        loadData()
        
        addRefresh()
        
        addAction()
        
    }
    
}

extension HWVipHeadPortraitFrameController {
    private func initUI() -> Void {
        navigationView.isHidden = true
        
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        view.addSubview(tableView)
    }
    
    private func initFrame() -> Void {
        tableView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}

extension HWVipHeadPortraitFrameController:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}

extension HWVipHeadPortraitFrameController {
    func loadData() -> Void {
        httpManager.getVipHeadProtraitList(typeId: type) { models in
            self.tableViewProtocol.models = models
            self.tableView.reloadData()
            
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            if models.count > 0 {
                self.modelArr.removeAllObjects()
                self.modelArr.addObjects(from: models)
            }
            self.tableViewProtocol.models = self.modelArr as? [HWHeadPortraitFrameModel]
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
        }
        
    }
    
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
//        tableView.mj_footer = MJRefreshBackNormalFooter()
//        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
    
    @objc func loadMoreData() -> () {
        self.tableView.mj_footer?.endRefreshing()
        self.httpManager.getVipHeadProtraitListFooter(typeId: type) { models in
            self.tableView.mj_footer?.endRefreshing()
            if models.count > 0 {
                self.modelArr.addObjects(from: models)
            }
            self.tableViewProtocol.models = self.modelArr as? [HWHeadPortraitFrameModel]
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_footer?.endRefreshing()
        }
        
    }
}
extension HWVipHeadPortraitFrameController {
    func addAction() -> Void {
        self.tableViewProtocol.receiveButtonClick = {
            id in
            self.httpManager.receiveVipGiftBag(typeId: self.type, id: id) { dic in
                self.loadData()
                self.callBack?()
            } failure: { error in
                
            }
        }
    }
}
